package com.lzx.lizhenxing.point.utils;

import com.lzx.lizhenxing.point.dao.PointDao;
import com.lzx.lizhenxing.point.model.Point;
import com.lzx.lizhenxing.sys.dao.UserDao;
import com.lzx.lizhenxing.sys.model.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
@Component
public class PointUtils {

    @Resource
    private PointDao pointDao;

    @Resource
    private UserDao userDao;

    /**
     * 积分表添加积分记录
     */
    public void insertPoint(Long uid, Integer pointNum, Integer pointType) {
        Point point = new Point();
        point.setUid(uid);
        point.setPoint(pointNum);
        point.setPointType(pointType);
        pointDao.insert(point);
    }

    /**
     * 积分表扣除积分记录
     */
    public void deletePoint(Long uid, Integer pointNum, Integer pointType){
        Point point = new Point();
        point.setUid(uid);
        point.setPoint(pointNum);
        point.setPointType(pointType);
        pointDao.insert(point);
        // 同时用户表的总积分减少相应的积分
        User user = userDao.selectById(uid);
        user.setPoint(user.getPoint() - pointNum);
        userDao.updateById(user);
    }


}
